package yy.robot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import yy.robot.domain.User;
import yy.robot.repository.UserRepository;

/**
 * Created by BaiJiFeiLong@gmail.com at 2018/5/7 11:19
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Page<User> findAll(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    public User findById(long id) {
        return userRepository.findById(id).get();
    }

    public User findByPhone(String phone) {
        return userRepository.findOneByPhone(phone);
    }

    public User findByUsername(String username) {
        return userRepository.findOneByUsername(username);
    }

    public User create(User user) {
        userRepository.save(user);
        return user;
    }

    public User update(User user) {
        userRepository.save(user);
        return user;
    }

    public void destroy(long id) {
        userRepository.deleteById(id);
    }
}
